package org.itboys.fuzion.service;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.itboys.fuzion.entity.ScoreLevel;
import org.itboys.fuzion.mysql.service.FuziongMemberStatisticsService;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseService;
import org.itboys.mongodb.utils.page.Page;
import org.itboys.seller.entity.Seller;
import org.itboys.seller.service.SellerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Maps;

/**
 * 积分等级定义
 * @author weisky
 *
 * 2016年4月15日
 */
@Service
public class ScoreLevelService extends BaseService<ScoreLevel, Long>{
	
	private static final long serialVersionUID = 1L;
	
	@Autowired
	private SellerService sellerService;
	@Autowired
	private FuziongMemberStatisticsService fuziongMemberStatisticsService;
	
	@Resource(name="baseMongoDS")
	private MongoDataSource ds;

	@Override
	protected MongoDataSource getMongoDataSource() {
		return ds;
	}
	
	/**
	 * 新增或者修改积分信息
	 * @param scoreLevel
	 * @return
	 */
	public boolean insert(ScoreLevel scoreLevel){
		try{
			if(scoreLevel.getId() != 0){
				super.update(scoreLevel);
				logger.info("修改积分等级成功：ScoreLevel=" + JSON.toJSONString(scoreLevel) + ", 修改时间 = " + new Date());
			}else{
				super.save(scoreLevel);
				logger.info("新增积分等级成功：ScoreLevel=" + JSON.toJSONString(scoreLevel) + ", 新增时间 = " + new Date());
			}
			return true;
		}catch(Exception e){
			e.printStackTrace();
			logger.error("ScoreLevelService.insert err", e);
		}
		return false;
	}
	
	/**
	 * 带有分页的组装积分等级信息
	 * @param param
	 * @param request
	 * @return
	 */
	public Page<ScoreLevel> listByPage(Map<String, Object> param, HttpServletRequest request){
		Page<ScoreLevel> page = super.pageQuery(param, request);
		if(page != null && page.getData() != null){
			page.getData().forEach(pd ->{
				Seller s = sellerService.getById(pd.getSellerId());
				if(s != null){
					pd.setSellerName(s.getName());
				}
				
				//组装当前会员人数
				List<Long> realPeopleNum = fuziongMemberStatisticsService.getMemberIdListByScoreId(pd.getId(), pd.getSellerId());
				if(realPeopleNum != null){
					pd.setRealPeopleNum(realPeopleNum.size());
				}else{
					pd.setRealPeopleNum(0);
				}
				
			});
		}
		return page;
	}

	/**
	 * 删除
	 * @param id
	 * @return
	 */
	public boolean dele(Long id){
		try{
			super.delete(id);
			logger.info("删除积分等级成功：ScoreLevel.id=" + id + ", 删除时间 = " + new Date());
			return true;
		}catch(Exception e){
			e.printStackTrace();
			logger.error("ScoreLevelService.dele error", e);
			return false;
		}
	}

	/**
	 * 根据传进来的积分,判断会员在哪一个
	 * @param score
	 * @return
     */
	public ScoreLevel getByScore(Integer score){
		Map<String, Object> param = Maps.newHashMapWithExpectedSize(2);
		param.put("startScore <", score);
		param.put("endScore >=", score);
		List<ScoreLevel> list = super.list(param);
		return list != null && !list.isEmpty() ? list.get(0) : null;
	}
}
